<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录确认</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, maximum-scale=1, user-scalable=no,viewport-fit=cover">
    <link href="css/common.css" rel="stylesheet">
    <style>
        .nav-title{
            width: 100%;
        }
    </style>
</head>
<body>
<div class="head-nav">
    <div class="nav-title">请确认</div>
</div>
<div class="main">
    <div class="notice-content">
        <div>您正在登录<span class="red-num" id="machine_name">未知机器</span></div>
        <div class="small">地址：<span id="shop_name"></span></div>
    </div>
    <div class="btn-line" id="goLogin">确认登录</div>
    <div class="tips">
        <div>提示：</div>
        <div>为了您的财产安全，离开机器时请退出。以下三种方式可以退出</div>
        <div>1.游戏机上点击“退出”</div>
        <div>2.公众号-个人中心-联机退出-退出登录</div>
        <div>3.一定时间未操作后系统自动退出</div>
    </div>
</div>

<script src="js/jquery-1.8.0.min.js"></script>
<script src="js/common.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="https://cdn.bootcss.com/layer/3.0.1/layer.min.js"></script>

<script type="application/javascript">
    var openid;
    var machineId;
    var WXuser;
    $(function () {
        machineId = Common.queryGetParam("machineId");
        getopenid();
        loadMachine(machineId);
    });
    $("#goLogin").on("click",function () {
        checkAttention();
        if(!machineId){

            layer.alert("机器码缺失，请联系管理人员！");
            return;
        }
        var paramStr = {};
        paramStr.wxOpenId = openid;
        paramStr.machineId = machineId;
        paramStr.headimgurl = WXuser.headimgurl;
        paramStr.nickname = WXuser.nickname;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);

        Common.ajaxWithParam("/fenful/api/v1/login/guaLogin", params, function(data) {
            if(data.userId){
                layer.msg("登录成功");
                window.location.href="myAccount.html";
            }
        });
    });

    function getopenid() {
        openid = window.localStorage.getItem("openid");
        if(openid == null || openid == undefined || openid == '') {
            getCode();
        }
    }
    function getCode() {
        var code = Common.queryGetParam('code');
        if(code == null || code == undefined || code == '') {
            location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95bdc635798fcfe4&redirect_uri="+window.location.href+"&response_type=code&scope=snsapi_base#wechat_redirect";
            return;
        }
        var paramStr = {};
        paramStr.code = code;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);
        Common.ajaxWithParam("/fenful/api/v1/common/getWxUserOpenId", params, function(data) {
            openid = data.openid;
            window.localStorage.setItem("openid", data.openid);
        },false);
    }
    //检查是否关注
    function checkAttention(){
        if(!openid){
            layer.alert("无法获取openid，请用微信打开连接");
            return;
        }
        var paramStr = {};
        paramStr.wxOpenId = openid;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);
        Common.ajaxWithParam("/fenful/api/v1/common/getUserInfoByOpenId", params, function(data) {
            var obj = JSON.parse(data);
            if(obj.subscribe!="1"){
                //未关注
                window.location.href="attentionGuide.html";
            }else{
                WXuser = obj;
            }
        },false);
    }

    //加载机器信息
    function  loadMachine(machineId) {
        if(!machineId){return;}
        var paramStr = {};
        paramStr.machineId = machineId;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);
        Common.ajaxWithParam("/fenful/api/v1/machine/getMachineAndShopById", params, function(data) {
            if(data.machine_name){
                $("#machine_name").text(data.machine_name);
                $("#shop_name").text(data.shop_name);
            }

        },false);
    }

</script>
</body>
</html>